<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php



class MAuthrization
    extends MApplicationComponent 
    implements MIAuthrization
{
    
    public function validAccessToken($token, $secret)
    {
        $MCache = new MCache();
        $user = $MCache->get($token);
        if ($user == false){
            $user = $this->oauthValidAccess($token, $secret);
        }
        return $user;
    }
    
    
    public static function oauthValidAccess($token, $secret) {
        $user_meta = MUser::queryUserMeatasBytoken($token, $secret);
        if ($user_meta == false || count($user_meta) < MConst::USER_METAS_LENGTH) {
            FXP::trace(FXP::t("user_meta's length < 2"));
            return false;
        }
                $user_info = new MUser();
        $user_id = $user_meta[0]['user_id'];
        
        $user    = MUser::queryUsersByID($user_id);
        if ($user == false || $user == null) {
            FXP::trace(FXP::t("user donot exist < 2"));
            return false;
        }
                $user_info = MUser::initUser($user[0]);
        $user_info->user_id = $user_id;
        $user_info->token  = $token;
        $user_info->secret = $secret;
        
        $team = MTeam::queryTeamByTeamId($user[0]['team_id']);
        if ($team == false || $team == null) {
            return false;
        }
        $user_info->team_name = $team[0]['name'];
        $user_info->team_uuid = $team[0]['team_uuid'];
                $user_privileges = MUser::queryUserPrivilegesByUserId($user_id);
        foreach ($user_privileges as $user_privilege) {
            $user_info->{$user_privilege["privilege_uuid"]} = $user_privilege["value"];
        }
                                $device_uuid = str_replace("token", "", $user_meta[0]["meta_key"]);
        $user_info->device_uuid = $device_uuid;
        return $user_info;
    }
}
?>